package com.simplecontact.activities;

import com.simplecontact.R;
import com.simplecontact.adapters.adSimpleText;
import com.simplecontact.classes.Calendar;
import com.simplecontact.classes.CalendarBundle;
import com.simplecontact.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class ActCalendar extends Activity {
	private ListView lsv4;	
	public CalendarBundle prepareToEditCalendar(int id){
		CalendarBundle cb = null;
		Cursor c = getContentResolver().query(Calendar.CalendarColumns.CONTENT_URI, null, null, null, null);				
        if (c.moveToFirst()) {        	
			int ColumnId = c.getColumnIndex(Calendar.CalendarColumns._ID);						
			int columnDateTime = c.getColumnIndex(Calendar.CalendarColumns.DATETIME);
			int columnContent = c.getColumnIndex(Calendar.CalendarColumns.CONTENT);
			int columnUrlMusic = c.getColumnIndex(Calendar.CalendarColumns.URLMUSIC);			
			do {
				// Get the field values								
				if(c.getInt(ColumnId)==id){														
					cb = new CalendarBundle(id, c.getLong(columnDateTime),c.getString(columnContent),c.getString(columnUrlMusic));
					break;
				}
			} while (c.moveToNext());			
		}
        return cb;
	}
	public String[] getCalendarContent(){
		Cursor c = getContentResolver().query(Calendar.CalendarColumns.CONTENT_URI, null, null, null, null);		
		String str = "";
        if (c.moveToFirst()) {        	
			int ColumnFisrt = c.getColumnIndex(Calendar.CalendarColumns.CONTENT);			
			do {
				// Get the field values								
				str += (c.getString(ColumnFisrt));
				str +="\n";
			} while (c.moveToNext());
		}
        String[] res = str.split("\n");
        if(res.length!=0)
        	return res;
        return null;
	}
	public int[] getCalendarId(){
		Cursor c = getContentResolver().query(Calendar.CalendarColumns.CONTENT_URI, null, null, null, null);		
		String str = "";
        if (c.moveToFirst()) {        	
			int ColumnId = c.getColumnIndex(Calendar.CalendarColumns._ID);			
			do {
				// Get the field values								
				str += (c.getString(ColumnId));
				str +="\n";							
			} while (c.moveToNext());
		}
        int[] res = null;
        if(str.length()>0)str = str.substring(0, str.length()-1);
        if(str.length()!=0){
        	String[] temp = str.split("\n");
        	res = new int[temp.length];		                    
        	for(int i = 0 ; i < res.length ; i++){
        		res[i] = Integer.parseInt(temp[i]);
        	}
        }
		return res;
	}
	public int deleteCalendar(int id){
		int row = getContentResolver().delete(Calendar.CalendarColumns.CONTENT_URI, "_id = " + id, null);
		lsv4 = (ListView)findViewById(R.id.listview4);				
		String[] arrContent = getCalendarContent();		
		int[] arrId = getCalendarId();
		if(arrId!=null)
			lsv4.setAdapter(new adSimpleText(getApplicationContext(), arrContent, arrId));
		else
			lsv4.setAdapter(null);
		return row;
	}
	protected void onStart ()
	{
		super.onStart();
		lsv4 = (ListView)findViewById(R.id.listview4);			
		String[] arrContent = getCalendarContent();					
		int[] arrId = getCalendarId();
		if(arrId!=null)
			lsv4.setAdapter(new adSimpleText(getApplicationContext(), arrContent, arrId));		
		else
			lsv4.setAdapter(null);
	}
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar);
        lsv4 = (ListView)findViewById(R.id.listview4);						
		String[] arrContent = getCalendarContent();					
		int[] arrId = getCalendarId();
		if(arrId!=null)
			lsv4.setAdapter(new adSimpleText(getApplicationContext(), arrContent, arrId));
	}
	@Override
    public boolean onPrepareOptionsMenu (Menu menu){
    	menu.clear();
    	menu.add(0, Utils.MENU_ADD_CALENDAR, 0, getString(R.string.strAdd)).setIcon(R.drawable.calendaradd32);
    	menu.add(0, Utils.MENU_EDIT_CALENDAR, 1, getString(R.string.strEdit)).setIcon(R.drawable.calendaredit32);
    	menu.add(0, Utils.MENU_DELETE_CALENDAR, 2, getString(R.string.strDelete)).setIcon(R.drawable.calendarerror32);
    	return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {    
    	// Handle item selection    
    	switch (item.getItemId()) {
    	case Utils.MENU_ADD_CALENDAR:    			
			Intent ic = new Intent(this, ActCreateCalendar.class);    			
			startActivity(ic);
			return true;    		 
		case Utils.MENU_EDIT_CALENDAR:    		
			if(lsv4 != null){
				if(lsv4.getSelectedItemId()>-1){
					Intent intent = new Intent(this, ActCreateCalendar.class);    	
					intent.putExtra("calendarbundle", prepareToEditCalendar((int)lsv4.getSelectedItemId()));
					startActivity(intent);
				}
				else{
					Toast.makeText(this, getString(R.string.strChooseToEdit), Toast.LENGTH_SHORT).show();
				}
			}
			return true;    		 
		case Utils.MENU_DELETE_CALENDAR:    	
			if(lsv4 != null){
				if(lsv4.getSelectedItemId()>-1){
					deleteCalendar((int)lsv4.getSelectedItemId());
				}
				else{
					Toast.makeText(this, getString(R.string.strChooseToDelete), Toast.LENGTH_SHORT).show();
				}
			}
			return true;
		default:        
			return super.onOptionsItemSelected(item);
    	}    	
	}    	
}
